# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify
import json
import random
import db_demo

app = Flask(__name__)


@app.route("/test/rnd")
def random_num():
    return str(random.randint(0, 10))


@app.route('/test/hello', methods=['POST'])
def reader_insert():
    data = request.get_data()
    json_dict = json.loads(data)

    return jsonify({"msg": "你好%s！" % (json_dict.get('name', ''))})


@app.route("/test/infolist")
def reader_menu():
    return """
        <h1>这是标题：</h1>
        <p>1、这是内容111111   </p> 
        <p>2、这是内容222222 </p> 
    """


@app.route('/test/save/<int:id>/', methods=['POST'])
def reader_update(id):
    data = request.get_data()
    json_dict = json.loads(data)

    print(json_dict)

    return jsonify({"code": "ok", "message": "操作成功！"})


@app.route("/test/getall")
def reader_getall():
    dict_list = [{
        "id": 1,
        "name": "测试"
    }, {
        "id": 2,
        "name": "测试22222"
    }]
    return jsonify(dict_list)


if __name__ == '__main__':
    app.config['JSON_AS_ASCII'] = False
    app.run(host='0.0.0.0', port=8888, debug=True)
